<template>
	<el-dialog v-model="dialog" :title="!state.form.id ? '新增' : '修改'" width="60%" :before-close="handleClose">
		<el-form ref="formRef" :model="state.form" :rules="state.rules" label-width="120px" @keyup.enter="submitForm(formRef)" status-icon>
	    				<el-form-item label="主键" prop="id">
					<el-input v-model="state.form.id" placeholder="主键"></el-input>
				</el-form-item>
				<el-form-item label="门店账号id" prop="sellerAccountId">
					<el-input v-model="state.form.sellerAccountId" placeholder="门店账号id"></el-input>
				</el-form-item>
				<el-form-item label="骑手id" prop="riderId">
					<el-input v-model="state.form.riderId" placeholder="骑手id"></el-input>
				</el-form-item>
				<el-form-item label="配送单id" prop="riderDistributionRefId">
					<el-input v-model="state.form.riderDistributionRefId" placeholder="配送单id"></el-input>
				</el-form-item>
				<el-form-item label="配送完成时间" prop="completeAt">
					<el-input v-model="state.form.completeAt" placeholder="配送完成时间"></el-input>
				</el-form-item>
				<el-form-item label="订单号" prop="orderNo">
					<el-input v-model="state.form.orderNo" placeholder="订单号"></el-input>
				</el-form-item>
				<el-form-item label="收益" prop="income">
					<el-input v-model="state.form.income" placeholder="收益"></el-input>
				</el-form-item>
				<el-form-item label="配送单完成状态" prop="status">
					<el-input v-model="state.form.status" placeholder="配送单完成状态"></el-input>
				</el-form-item>
				<el-form-item label="收益来源" prop="source">
					<el-input v-model="state.form.source" placeholder="收益来源"></el-input>
				</el-form-item>
				<el-form-item label="配送费" prop="distributionFee">
					<el-input v-model="state.form.distributionFee" placeholder="配送费"></el-input>
				</el-form-item>
				<el-form-item label="配送距离" prop="distributionDistance">
					<el-input v-model="state.form.distributionDistance" placeholder="配送距离"></el-input>
				</el-form-item>
				<el-form-item label="创建时间" prop="createTime">
					<el-input v-model="state.form.createTime" placeholder="创建时间"></el-input>
				</el-form-item>
				<el-form-item label="更新时间" prop="updateTime">
					<el-input v-model="state.form.updateTime" placeholder="更新时间"></el-input>
				</el-form-item>
				<el-form-item label="逻辑删除：0.显示；1.隐藏；" prop="delFlag">
					<el-input v-model="state.form.delFlag" placeholder="逻辑删除：0.显示；1.隐藏；"></el-input>
				</el-form-item>
				<el-form-item label="创建人" prop="createBy">
					<el-input v-model="state.form.createBy" placeholder="创建人"></el-input>
				</el-form-item>
				<el-form-item label="修改人" prop="updateBy">
					<el-input v-model="state.form.updateBy" placeholder="修改人"></el-input>
				</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">关 闭</el-button>
				<el-button type="primary" :loading="loading" @click="submitForm(formRef)">确 认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage,FormInstance } from 'element-plus'
import { addObj, editObj, getById } from '/@/api/cloud/rider_income'
const emit = defineEmits(['initPage']);
const state = reactive({
	form: {
		id: '',
		sellerAccountId: '',
		riderId: '',
		riderDistributionRefId: '',
		completeAt: '',
		orderNo: '',
		income: '',
		status: '',
		source: '',
		distributionFee: '',
		distributionDistance: '',
		createTime: '',
		updateTime: '',
		delFlag: '',
		createBy: '',
		updateBy: ''	},
	rules: {
	},
})


const dialog = ref(false);
const loading = ref(false);
const formRef = ref();

const initForm = (row: any | undefined) => {
	dialog.value = true;
	if (row && row.id) {
		getDetail(row.id);
	}
};

const getDetail = (id: string) => {
	loading.value = true;
	getById(id).then((response: any) => {
		state.form = response.data.data;
		loading.value = false;
	});
};

/**
 * 关闭事件
 */
const handleClose = () => {
	resetForm(formRef.value);
};

/**
 * 重置表单
 */
const resetForm = (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	state.form.id = '';
	loading.value = false;
	dialog.value = false;
	formEl.resetFields();
};


/**
 * 提交按钮
 */
const submitForm = async (formEl: FormInstance | undefined) => {
	if (!formEl) return;
	await formEl.validate((valid) => {
		if (valid) {
			loading.value = true;
			if (state.form.id) {
				//修改
				edit();
			} else {
				//新增
				add();
			}
		}
	});
};
/**
 * 新增
 */
const add = () => {
	addObj(state.form)
			.then(() => {
				resetForm(formRef.value);
				ElMessage.success('新增成功');

				emit('initPage');
			})
			.catch(() => {
				loading.value = false;
			});
};
/**
 * 修改
 */
const edit = () => {
	editObj(state.form)
			.then(() => {
				resetForm(formRef.value);
				ElMessage.success('修改成功');
				emit('initPage');
			})
			.catch(() => {
				loading.value = false;
			});
};

defineExpose({
	initForm
})
</script>
